const communityService = require('../../service/web/community');

const communityController = {
    getList: async (req, res) => {
        const { page = 1, pageSize = 9, keyword = '', category = 'all', sort = 'latest' } = req.query;
        const skip = (parseInt(page) - 1) * parseInt(pageSize);
        const limit = parseInt(pageSize);
        const { data, total } = await communityService.getList(skip, limit, { keyword, category, sort });
        res.send({ code: 200, message: '获取成功', data, total, page: parseInt(page), pageSize: limit });
    },
    getDetail: async (req, res) => {
        const { id } = req.params;
        await communityService.addView(id);
        const detail = await communityService.getDetail(id);
        res.send({ code: 200, message: '获取成功', data: detail });
    },
    getRecommend: async (req, res) => {
        const data = await communityService.getRecommend(6);
        res.send({ code: 200, message: '获取成功', data });
    },
    getHot: async (req, res) => {
        const data = await communityService.getHot(6);
        res.send({ code: 200, message: '获取成功', data });
    },
    like: async (req, res) => {
        const { id } = req.query;
        await communityService.like(id);
        res.send({ code: 200, message: '点赞成功' });
    },
    getCommentList: async (req, res) => {
        const { id } = req.params;
        const { page = 1, pageSize = 10 } = req.query;
        const skip = (parseInt(page) - 1) * parseInt(pageSize);
        const limit = parseInt(pageSize);
        const { data, total } = await communityService.getCommentList(id, skip, limit);
        res.send({ code: 200, message: '获取成功', data, total });
    },
    addComment: async (req, res) => {
        try {
            // 验证必填参数
            const { postId, articleID, nickname, content, address } = req.body;
            
            if (!postId && !articleID) {
                return res.status(400).send({
                    code: 400,
                    message: '缺少文章ID参数'
                });
            }
            
            if (!nickname || !content) {
                return res.status(400).send({
                    code: 400,
                    message: '缺少必填参数：昵称和评论内容'
                });
            }
            
            // 验证文章是否存在
            const articleId = postId || articleID;
            const article = await communityService.getArticleById(articleId);
            if (!article) {
                return res.status(404).send({
                    code: 404,
                    message: '文章不存在'
                });
            }
            
            await communityService.addComment(req.body);
            res.send({ code: 200, message: '评论成功' });
        } catch (error) {
            console.error('发表评论错误:', error);
            res.status(500).send({
                code: 500,
                message: '服务器内部错误'
            });
        }
    },
    search: async (req, res) => {
        const { keyword = '' } = req.query;
        const data = await communityService.search(keyword);
        res.send({ code: 200, message: '获取成功', data });
    },
    
    // 获取统计数据
    getStatistics: async (req, res) => {
        const data = await communityService.getStatistics();
        res.send({ code: 200, message: '获取统计数据成功', data });
    },
    
    // 获取分类列表
    getCategories: async (req, res) => {
        const data = await communityService.getCategories();
        res.send({ code: 200, message: '获取分类列表成功', data });
    },
};

module.exports = communityController;


